// favorite.js
const app = getApp()
import { requestApi } from '../../../api'
import { formatImage } from '../../../utils/formatImage'
import { formatPrice } from '../../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    channel: '',
    loading: false,
    start: 0,
    count: 20,
    goodsArr: [],
    requestFlag: false,
    noMore: false,
    noData: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channel = options && options.channel || 'wx_xcx'

    this.data.channel = channel
    this.getFavoriteData(this.data.start, channel)

    let systemInfo = {}

    try {
      systemInfo = wx.getSystemInfoSync()
    } catch (e) {
      // Do something when catch error
    }

    this.setData({
      winWidth: systemInfo.windowWidth,
      winHeight: systemInfo.windowHeight,
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getFavoriteData(this.data.start, this.data.channel)
  },

  toDetail: function(e) {
    let gid = e.currentTarget.id
    let url = '../../goods/goodsDetail/goodsDetail?gid=' + gid + '&channel=' + this.data.channel

    wx.navigateTo({
      url,
    })
  },

  // 请求数据
  getFavoriteData(start, channel) {
    if (this.data.noMore) return false
    
    app.request({
      url: requestApi.favoriteListUrl,
      data: {
        start: start,
        count: this.data.count,
        channel: channel,
      },
    }, (data) => {
      data = this.formatData(data)
      
      let noMore, noData
      let currArr = this.data.goodsArr
      let goodsArr = currArr.concat(data.original.goods || [])

      if (goodsArr.length == 0) {
        noData = true
      }
      if (data.original.page_count < this.data.count) {
        noMore = true
      } else {
        noMore = false

        this.data.requestFlag = true
      }
      
      this.setData({
        loading: true,
        goodsArr: goodsArr,
        start: this.data.start + this.data.count,
        noMore: noMore,
        noData: noData,
        requestFlag: this.data.requestFlag,
      })
    }, (error) => {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: error,
      })
    })
  },

  // 处理请求返回的数据
  formatData: function (data) {
    data.original.page_count = 0

    if (data.original.goods && data.original.goods.length) {
      for (let i = 0, len = data.original.goods.length; i < len; i++) {
        if (data.original.goods[i].product_cover_image && data.original.goods[i].product_cover_image.path) {
          data.original.goods[i].product_cover_image.new_path = formatImage.cdnImage(data.original.goods[i].product_cover_image.path)
        }

        if (data.original.goods[i].goods_status_icon && data.original.goods[i].goods_status_icon.path) {
          data.original.goods[i].goods_status_icon.full_path = formatImage.cdnImage(data.original.goods[i].goods_status_icon.path, formatImage.THUMBNAIL_XS)
        }

        if (data.original.goods[i].global_icon && data.original.goods[i].global_icon.path) {
          data.original.goods[i].global_icon.full_path = formatImage.cdnImage(data.original.goods[i].global_icon.path, formatImage.THUMBNAIL_M)
        }

        data.original.goods[i].new_goods_price = data.original.goods[i].goods_price ? formatPrice(data.original.goods[i].goods_price / 100) : 0
        data.original.goods[i].new_original_price = data.original.goods[i].original_price ? formatPrice(data.original.goods[i].original_price / 100) : 0
      }
      data.original.page_count = data.original.goods.length
    }

    return data
  },
})